<template>
  <div align="center"><h1>展示新闻</h1>
    <table border="1" cellspacing="0">
      <tr>
        <td>新闻</td>
        <td>价格</td>
        <td>详情</td>
      </tr>
      <tr v-for="i in news">
        <td>{{ i.name }}</td>
        <td>{{ i.price }}</td>
        <td>
          <button @click="jump(i.id)">查看</button>
        </td>
      </tr>

    </table>
    <button v-for="i in tpage" @click="get_news(i)">{{ i }}</button>


  </div>
</template>

<script>
export default {
  name: "show_news",
  data() {
    return {
      news: [],
      tpage: 0,
      userid: localStorage.getItem('userid'),

    }
  },
  methods: {
    get_news(i) {
      this.$axios.get('news/?cpage=' + i)
        .then(res => {
          if (res.data.code == 200) {
            this.news = res.data.list
            this.tpage = res.data.tpage

          }
        })
    },
    jump(id) {
      if (this.userid) {

        this.$axios.post('quanxian/', {'userid': this.userid, 'newsid': id})
          .then(res => {
            if (res.data.code == 200) {
              this.$router.push({name: 'delite', query: {'id': id}})
            } else {
              alert('已达到最大访问次数')
            }
          })


      } else {
        alert('未登录')
        this.$router.push({name: 'nlogin', query: {'id': id}})
      }

    }
  },
  mounted() {
    this.get_news(1)
  }
}
</script>

<style scoped>

</style>
